import {
  getInfo
} from '../../../api/shareCenter';
import {
  transfer,calculate
} from '../../../api/point';
const util = require('../../../utils/util.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    point:0,
	balance: 0,
	amount: '',
	maxlength: 10,
	disabled: true,
	flag: true,
	wrapAnimate: 'wrapAnimate',
    bgOpacity: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	this.getBalance()
  },
  getBalance () {
    getInfo().then(res => {
      let point = res.data.point .toFixed(2)
	  this.setData({ point })
    }).catch(err => {
      console.log(err)
    })
  },
  getInput(e) {
		let amount = e.detail.value
		let maxlength = amount.indexOf('.') + 3 // 限制小数点后只能输入两位
		if(maxlength == 2){
			maxlength = 10
		}
		let disabled = util.isNull(amount)
		// amount = Number(amount)
		if (disabled) {
			amount = ''
		} else {
			this.cashOut()
		}
		this.setData({ amount, maxlength, disabled })
  },
  wholeCashOut () {
	let amount = this.data.point/100
	this.setData({ amount,disabled:false })
  },
addbank() {
	wx.navigateTo({
		url: '/pages/member/distribution/addbank'
	})
	},
checkBank() {
	this.selectComponent('#bottomFrame').showFrame("3329", "buyNow");
},

  cashOut () {
	 if (this.data.amount > this.data.point/100) {
		wx.showToast({ title: '余额不足', icon: 'none', duration: 2000 })
	} else if (this.data.amount == 0) {
		wx.showToast({ title: '提现金额不能为0', icon: 'none', duration: 2000 })
	} else if (this.data.amount < 0) {
		wx.showToast({ title: '提现金额不能为负数', icon: 'none', duration: 2000 })
	} else if (isNaN(this.data.amount)) {
		wx.showToast({ title: '请输入正确格式', icon: 'none', duration: 2000 })
	} else {
		calculate(this.data.amount).then(res => {
			wx.showModal({
			  title: '提示',
			  content: `提现金额${this.data.amount}元, 去除手续费${res.data}之后可获取${this.data.amount - res.data}元`,
			  success: (res) => {
				if (res.confirm) {
					let params = {
					  amount: this.data.amount,
					  paymentPluginId: 'weixinWalletPlugin'
					}
					transfer(params).then(res => {
					  console.log(res)
					  wx.showToast({ title: '已成功提现', icon: 'success', duration: 2000, mask: true })
					  setTimeout(() => {
						wx.navigateBack({ delta: 1 })
					  }, 1000)
					}).catch(err => { console.log(err) })
				} else if (res.cancel) {
				  wx.showToast({ title: '已取消', icon: 'none', duration: 2000 })
				}
			  }
			})
		}).catch(err => { console.log(err) })
	}
  },

  shopList(){
	  console.log("3132131")
	  wx.navigateTo({
		url: '/pages/member/distribution/shopList'
	  })
  },
  transfer1(){
	console.log("3132131")
	wx.navigateTo({
	  url: '/pages/member/distribution/transfer1'
	})
},
transfer1(){
	console.log("3132131")
	wx.navigateTo({
	  url: '/pages/member/distribution/register'
	})
},
time(){
	console.log("3132131")
	wx.navigateTo({
	  url: '/pages/member/distribution/time'
	})
}
})